using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_CtrlPersonal : System.Web.UI.UserControl
{
    protected Account acc;
    protected Functions func;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadInformPersonal();
        }
    }
    private void loadInformPersonal()
    {
        acc = new Account();
        int ID = Convert.ToInt32(Session["ID"].ToString());
        acc = acc.GetUser(ID);
        txtName.Text = acc.name;
        txtEmail.Text = acc.email;
        txtAddress.Text = acc.address;
        txtNumberID.Text = acc.identify_card;
        DateTime tmpDate = acc.birthday;
        txtDate.Text = tmpDate.Day.ToString();
        ddlMonth.SelectedValue = tmpDate.Month.ToString();
        txtYear.Text = tmpDate.Year.ToString();
        hdEmail.Value = acc.email;
    }
    //validate ID, name, birthday
    protected void txtIDCustomValidate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;
    }

    //validate email
    protected void txtEmailCustomValidate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        acc = new Account();
        acc.email = txtEmail.Text;
        if (acc.checkEmail() && txtEmail.Text != "" && hdEmail.Value != txtEmail.Text)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            acc = new Account();
            func = new Functions();
            acc.name = txtName.Text;
            acc.birthday = func.getDateTime(txtDate.Text, ddlMonth.SelectedValue, txtYear.Text);
            acc.identify_card = txtNumberID.Text;
            acc.address = txtAddress.Text;
            acc.email = txtEmail.Text;
            acc.type_user = 3;
            acc.userid = Convert.ToInt32(Session["ID"].ToString());
            if (acc.UpdateAccount())
            {
                Response.Redirect("./Default.aspx");
            }
        }
    }
}
